package com.bys.admin.ai.controller;

import com.bys.admin.ai.controller.vo.ChatPageReqVO;
import com.bys.admin.ai.controller.vo.ChatSaveReqVO;
import com.bys.admin.ai.entity.Chat;
import com.bys.admin.ai.service.ChatService;
import com.bys.response.R;
import com.bys.security.SecurityUtils;
import com.bys.util.BeanUtils;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.validation.constraints.*;
import javax.validation.*;
import java.util.*;
import java.io.IOException;

import com.bys.vo.PageVo;
import static com.bys.response.R.success;

@Tag(name = "管理后台 - AI Chat 对话")
@RestController
@RequestMapping("/ai/chat")
@Validated
public class ChatController {

    @Resource
    private ChatService chatService;
    @DeleteMapping("/delete-by-unpinned")
    @Operation(summary = "删除未置顶的聊天对话")
    public R<Boolean> deleteChatConversationMyByUnpinned() {
        chatService.deleteChatMyByUnpinned(SecurityUtils.getUserId());
        return success(Boolean.TRUE);
    }
    @GetMapping("/my-list")
    @Operation(summary = "获得【我的】聊天对话列表")
    public R<List<Chat>> getChatConversationMyList() {
        List<Chat> list = chatService.listByUserId(SecurityUtils.getUserId());
        return success(list);
    }
    @Operation(summary = "创建【我的】聊天对话")
    @PostMapping("/create-my")
    public R<Long> createChatConversationMy(@RequestBody @Valid ChatSaveReqVO createReqVO) {
        return success(chatService.createChatMy(createReqVO, SecurityUtils.getUserId()));
    }

    @PostMapping("/create")
    @Operation(summary = "创建AI Chat 对话")
    public R<Boolean> createChat(@Valid @RequestBody ChatSaveReqVO createReqVO) {
    chatService.createChat(createReqVO);
    return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI Chat 对话")
    public R<Boolean> updateChat(@Valid @RequestBody ChatSaveReqVO updateReqVO) {
        chatService.updateChat(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除AI Chat 对话")
    @Parameter(name = "id", description = "编号", required = true)
    public R<Boolean> deleteChat(@PathVariable Long id) {
        chatService.deleteChat(id);
        return success(true);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "获得AI Chat 对话")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public R<Chat> getChat(@PathVariable Long id) {
        Chat Chat = chatService.getChat(id);
        return success(Chat);
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI Chat 对话分页")
    public R<Page<Chat>> getChatPage(@Valid ChatPageReqVO pageReqVO) {
        Page<Chat> pageResult = chatService.getChatPage(pageReqVO);
        return success(pageResult);
    }
}